﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    public class UserViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private UserModel gUserModel;
        

        public UserViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gUserModel = new UserModel();

            UpdateList();
        }

        public void UpdateList()
        {
            DataList.Clear();

            List<WUser> list = WUser.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                LUser item = new LUser();
                item.IsChoose = false;
                item.OpenID = list[i].OpenID;
                item.UserName = list[i].UserName;
                item.Password = list[i].Password;
                item.Nickname = list[i].Nickname;
                item.Sex = list[i].Sex;
                item.Province = list[i].Province;
                item.City = list[i].City;
                item.Country = list[i].Country;
                item.Headimgurl = list[i].Headimgurl;
                item.Unionid = list[i].Unionid;
                if(item.Sex == 0)
                {
                    item.Sexer = "男";
                }
                else if (item.Sex == 1)
                {
                    item.Sexer = "女";
                }
                else
                {
                    item.Sexer = "其他";
                }
                item.SexerSelected = item.Sexer;
                DataList.Add(item);
            }
        }

        public ObservableCollection<LUser> DataList
        {
            get
            {
                return gUserModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        public ObservableCollection<LUser> DataListEdit
        {
            get
            {
                return gUserModel.DataListEdit;
            }

            set
            {
                DataListEdit = value;
                RaisePropertyChanged("DataListEdit");
            }
        }

        public Visibility DataListEditVisibility
        {
            get
            {
                return gUserModel.dataListEditVisibility;
            }
            set
            {
                gUserModel.dataListEditVisibility = value;
                RaisePropertyChanged("DataListEditVisibility");
            }
        }

        protected override void DLCloseCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLSaveCommand(object obj)
        {
            List<WUser> list = new List<WUser>();
            foreach (LUser item in DataListEdit)
            {
                if (item.IsChoose)
                {
                    list.Add(new WUser(item));
                }
            }
            HttpResponse response = WUser.Update(list);

            UpdateList();

            DataListEditVisibility = Visibility.Hidden;
            DLBSaveVisibility = Visibility.Hidden;
            EditMode = false;
        }

        protected override void DLQuitCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLGoCommand(object obj)
        {

        }

        protected override void DLPageUpCommand(object obj)
        {

        }

        protected override void DLPageDownCommand(object obj)
        {

        }

        protected override void DLCheckedCommand(object obj)
        {
            if (DLTIsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void DLAddCommand(object obj)
        {
            UserInsert window = new UserInsert();
            UserInsertViewModel viewModel = new UserInsertViewModel(window, this);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected override void DLEditCommand(object obj)
        {
            if(!EditMode)
            {
                DataListEdit.Clear();

                foreach (LUser item in DataList)
                {
                    if(item.IsChoose)
                    {
                        item.SexerSelected = item.Sexer;
                        DataListEdit.Add(item);
                    }
                }

                if(DataListEdit.Count>0)
                {
                    DataListEditVisibility = Visibility.Visible;
                    DLBSaveVisibility = Visibility.Visible;
                    EditMode = true;
                }
            }
        }

        protected override void DLDeleteCommand(object obj)
        {
            List<WUser> list = new List<WUser>();
            foreach (LUser item in DataList)
            {
                if (item.IsChoose)
                {
                    list.Add(new WUser(item));
                }
            }

            HttpResponse response = WUser.Delete(list);

            UpdateList();
        }
    }
}
